﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;

public partial class staffpages_ManageSurvey : System.Web.UI.Page
{
    XmlDocument doc = new XmlDocument();

    protected void Page_Load(object sender, EventArgs e)
    {
        loadDatafromXml();
        resetForm();
        
    }

    public void loadDatafromXml()
    {
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(Server.MapPath("~/xml/survey.xml"));
            
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtQuestion.Text == "")
        {
            lblQues.Text = "Question must not empty";
        }
        else if (txtAns1.Text == "")
        {
            lblAns1.Text = "Answer 1 must not empty";
        }
        else if (txtAns2.Text == "")
        {
            lblAns2.Text = "Answer 2 must not empty";
        }
        else if (txtAns3.Text == "")
        {
            lblAns3.Text = "Answer 3 must not empty";
        }
        else if (txtAns4.Text == "")
        {
            lblAns4.Text = "Answer 4 must not empty";
        }
        else
        {
            doc.Load(Server.MapPath("~/xml/survey.xml"));
            XmlElement question = doc.CreateElement("question");
            XmlElement ans1 = doc.CreateElement("ans1");
            XmlElement ans2 = doc.CreateElement("ans2");
            XmlElement ans3 = doc.CreateElement("ans3");
            XmlElement ans4 = doc.CreateElement("ans4");

            XmlAttribute quesId = doc.CreateAttribute("id");
            XmlAttribute text = doc.CreateAttribute("question");

            XmlNodeList xmlNL = doc.GetElementsByTagName("question");
            int count = xmlNL.Count + 1;

            quesId.Value = count.ToString();
            text.Value = txtQuestion.Text;

            ans1.InnerText = txtAns1.Text;
            ans2.InnerText = txtAns2.Text;
            ans3.InnerText = txtAns3.Text;
            ans4.InnerText = txtAns4.Text;

            question.Attributes.Append(quesId);
            question.Attributes.Append(text);
            question.AppendChild(ans1);
            question.AppendChild(ans2);
            question.AppendChild(ans3);
            question.AppendChild(ans4);


            doc.DocumentElement.AppendChild(question);

            doc.Save(Server.MapPath("~/xml/survey.xml"));
        }
        loadDatafromXml();
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        doc.Load(Server.MapPath("~/xml/survey.xml"));
        XmlNode node = doc.SelectSingleNode("survey/question[@id = '" + lblID.Text + "']");
        if (node != null)
        {
            node.Attributes["question"].Value = txtQuestion.Text;
            node.ChildNodes[0].InnerText = txtAns1.Text;
            node.ChildNodes[1].InnerText = txtAns2.Text;
            node.ChildNodes[2].InnerText = txtAns3.Text;
            node.ChildNodes[3].InnerText = txtAns4.Text;

            doc.Save(Server.MapPath("~/xml/survey.xml"));
        }
        loadDatafromXml();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        resetForm();
    }

    private void resetForm()
    {
        txtAns1.Text = "";
        txtAns2.Text = "";
        txtAns3.Text = "";
        txtAns4.Text = "";
        txtQuestion.Text = "";
        lblID.Text = "";
        lblAns1.Text = "";
        lblAns2.Text = "";
        lblAns3.Text = "";
        lblAns4.Text = "";
        lblQues.Text = "";
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow gvr = GridView1.SelectedRow;

        txtQuestion.Text = gvr.Cells[7].Text.Trim();
        txtAns1.Text = gvr.Cells[2].Text.Trim();
        txtAns2.Text = gvr.Cells[3].Text.Trim();
        txtAns3.Text = gvr.Cells[4].Text.Trim();
        txtAns4.Text = gvr.Cells[5].Text.Trim();

        //bind ID
        doc.Load(Server.MapPath("~/xml/survey.xml"));
        int i = gvr.RowIndex;
        XmlNode node = doc.DocumentElement.ChildNodes[i];
        lblID.Text = node.Attributes["id"].Value;
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        for (int i = 2; i < 7; i++)
        {
            e.Row.Cells[i].Visible = false;
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        using (DataSet ds = GridView1.DataSource as DataSet)
        {
            ds.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
            ds.WriteXml(Server.MapPath("~/xml/survey.xml"));
        }
    }
}